package com.falser.order.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 订单贸易状态mq配置
 *
 * @author falser
 * @date 2023/03/28
 */
@Configuration
public class OrderTradeStatusMqConfig {

    @Value("${mq.order.trade.status.exchange}")
    private String delayExchangeName;

    @Value("${mq.order.trade.status.queue}")
    private String timeoutTradeQueueName;

    @Value("${mq.order.trade.status.routeKey}")
    private String routeKey;

    @Bean
    public Queue orderTradeStatusQueue() {
        return new Queue(timeoutTradeQueueName, true);
    }


    /**
     * 绑定延时队列与交换机
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding orderTradeStatusBind() {
        return BindingBuilder.bind(orderTradeStatusQueue()).to(orderTradeStatusExchange()).with(routeKey);
    }

    /**
     * 订单贸易状态交换机
     *
     * @return {@link TopicExchange}
     */
    @Bean
    public TopicExchange orderTradeStatusExchange() {
        TopicExchange topicExchange = new TopicExchange(delayExchangeName, true, false, null);
        topicExchange.setDelayed(true);
        return topicExchange;
    }
}
